﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Common
{
    public static  class ACCVoucher
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(ACC_Voucher voucher)
        {
            db.AddToACC_Voucher(voucher);
            
            db.SaveChanges();
            return voucher.Number ;
        }
        public static ACC_Voucher  Single(long id)
        {
            return db.ACC_Voucher.Single(x => x.Number == id);
        }


        public static IEnumerable<ACC_Voucher> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from voucher in db.ACC_Voucher select voucher ;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Number")
                {
                    long Number;
                    Number = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Number == Number);
                }
                if (item.Key == "Student_Id")
                {
                    long Student_Id = Convert.ToInt16(item.Value);
                    query = query.Where(c => c.Student_Id == Student_Id);
                }
                if (item.Key == "Payment_Order_Number")
                {
                    long payNo;
                    payNo = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Payment_Order_Number == payNo);
                }
                if (item.Key == "Date")
                {
                    DateTime mydate;
                    mydate = Convert.ToDateTime(item.Value);
                    query = query.Where(c => c.Date == mydate );
                }
            }
            return query.ToList();
        }

        public static void Update(ACC_Voucher voucher)
        {

            ACC_Voucher c = db.ACC_Voucher.Single(x => x.Number == voucher.Number);
            c.Number = voucher.Payment_Order_Number;
            c.Date = voucher.Date;

            c.Student_Id = voucher.Student_Id;
            c.File_Number = voucher.File_Number;
            c.Payment_Order_Number = voucher.Payment_Order_Number;
            c.Expense_Id = voucher.Expense_Id;
            c.Value = voucher.Value;
            c.Detail = voucher.Detail;
            c.Is_Active = voucher.Is_Active;
            c.Is_Closed = voucher.Is_Closed;

            db.SaveChanges();

        }
        public static IEnumerable<ACC_Payment_Type> GetPaymentTypes()
        {
            return (from c in db.ACC_Payment_Type  orderby c.Name  select c);
        }

        public static Boolean  Delete(long id)
        {
            try
            {
                db.ACC_Voucher.DeleteObject(db.ACC_Voucher.Single(x => x.Number == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
